'use strict';
const jwt = require('jsonwebtoken');

const verifyPath = [
  '/api/user/info',
  '/api/address',
  '/api/car',
  '/api/car/add',
];
// 登录鉴权
module.exports = () => {
  return async (ctx, next) => {
    // const token = ctx.request.header.token;
    const token = ctx.cookies.get('token');
    if (verifyPath.indexOf(ctx.request.path) !== -1) {
      if (!token) {
        ctx.throw(401, '用户未登录');
      } else {
        // try {
        const user = jwt.verify(token, 'liuyu001');
        ctx.userInfo = user;
        await next();
        // } catch (e) {
        //   ctx.throw(401, 'token过期失效');
        // }
      }
    } else {
      await next();
    }
  };
};
